import { GetServerSideProps, NextPage, PageConfig } from 'next';
import Head from 'next/head';
import Content from './content';
import { Query as Q1 } from '../../api/professional/course/query.api';
import { tbgroup, tbmajor } from '../../../atoms/db';

interface IProps {
	query: Q1;
	majors: Pick<ITbmajor, 'majorname' | 'major_id'>[];
	groups: Pick<ITbgroup, 'group_id' | 'groupname'>[];
}

/**
 * 管理课程
 */
const Page: NextPage<IProps> = ({ query, majors, groups }) => {
	return (
		<>
			<Head>
				<title>管理课程</title>
			</Head>
			<Content initialquery={query} majors={majors} groups={groups} />
		</>
	);
};

export const config: PageConfig = {
	amp: false
};

export default Page;

// pre-render this page on each request
export const getServerSideProps: GetServerSideProps<IProps> = async (context) => {
	const query = context.query as Record<string, string>;
	const majors = await tbmajor().select('majorname', 'major_id').orderBy('majorname', 'asc');
	const groups = await tbgroup().select('group_id', 'groupname').orderBy('groupname', 'asc');
	return {
		props: {
			majors,
			groups,
			query: {
				page: query.page || '1',
				pagesize: '10',
				q: query.q || ''
			}
		}
	};
};
